package com.example.myapplication;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.myapplication.MyHelper;
import androidx.appcompat.app.AppCompatActivity;

public class RegistActivity extends AppCompatActivity {
    private Button btn;
    private EditText pwd,phone;
    String account,password;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        MyHelper  myHelper = new MyHelper(this);
        pwd=findViewById(R.id.pwd);
        phone=findViewById(R.id.phone);
        btn = findViewById(R.id.next);

        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                password=pwd.getText().toString();
                account=phone.getText().toString();
                if (password.equals("")||account.equals("")) {
                    Toast.makeText(RegistActivity.this, "请您将信息填写完整！", Toast.LENGTH_LONG).show();
                }else if(password.length()<6||account.length()<6){
                    Toast.makeText(RegistActivity.this, "密码和账号长度在6位及以上请检查", Toast.LENGTH_LONG).show();
                }else{
                    SQLiteDatabase db ;
                    ContentValues values;
                    db = myHelper.getWritableDatabase();
                    values = new ContentValues();
                    values.put("phone",account);
                    values.put("pwd",password);
                    db.insert("user",null,values);
                    Toast.makeText(RegistActivity.this,"注册成功！",Toast.LENGTH_SHORT).show();
                    db.close();
                    Intent intent=new Intent();
                    intent.setClass(RegistActivity.this,MainActivity.class);
                    startActivity(intent);
                }
            }
        });


    }
}
